package xman.mssh.spring;

import java.lang.reflect.Proxy;

import org.junit.Test;

import xman.mssh.spring.proxy.cglib.CglibProxy;
import xman.mssh.spring.proxy.dynamic.ProxyHandler;
import xman.mssh.spring.proxy.dynamic.Target;
import xman.mssh.spring.proxy.dynamic.TargetImpl;

public class ProxyTestClient {

    @Test
    public void dynamicProxy() {
        
        Target target = new TargetImpl();
        
        ProxyHandler handler = new ProxyHandler(target);// wave target and proxyHandler
        
        /*-
         * Proxy.newProxyInstance(ClassLoader loader, Class[] interfaces, InvocationHandler h)
         * JDK本身的局限性，只提供接口的代理，而不支持类的代理,
         */
        Target proxy = (Target) Proxy.newProxyInstance(target.getClass().getClassLoader(), target.getClass()
                .getInterfaces(), handler); // newProxyInstance
        
        proxy.outString("DynamicProxy");
    }
    
    @Test
    public void cglibProxy() {
        CglibProxy cglibProxy = new CglibProxy();
        TargetImpl proxy = (TargetImpl) cglibProxy.getProxy(TargetImpl.class);
        proxy.outString("CglibProxy");
        
    }
}
